package cn.edu.nuc.onlinestore.network;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Set;

import cn.edu.nuc.onlinestore.model.Carts;
import cn.edu.nuc.onlinestore.model.Client;
import cn.edu.nuc.onlinestore.model.Goods;
import cn.edu.nuc.onlinestore.vo.Result;
import demo.factor.Ifactor.IFactory;

public class TCPServer extends Thread {
	
	private ServerSocket server = null;
	public TCPServer(){
		
		try {
			server = new ServerSocket( 8080 );
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	@Override
	public void run() {
		//
		Socket client = null;
		ObjectInputStream reader = null;
		ObjectOutputStream writer = null;
		
			while(true){
				try {
					//阻塞等待客户端连接
					client = server.accept();
					//读取数据
					reader = new ObjectInputStream(client.getInputStream());
					Result<Object>  result = (Result)reader.readObject();//收到客户端消息
					
					Result<Object> rs = null;
					switch( result.getMsg() ){
						case "findallgood":
							Set<Goods> set=IFactory.selectAllGoods();
							rs=new Result<Object>(false, null, null);
							rs.setObj(set);
							break;
						case "findaclient":
							Client c=IFactory.selectAClient(result.getObj().toString());
							if(c!=null)
							{
								rs=new Result<Object>(true,"这个用户找到了",c);
							}else{
								rs=new Result<Object>(false,"用户不存在",null);
							}
							break;
						case "registerclient":
							Client cl=(Client)result.getObj();
							System.out.println("clclclclclclcllclclclc"+cl);
							boolean b=IFactory.addClient(cl);
							rs=new Result<Object>(false,"注册失败",null);
							rs.setCode(b);
							break;
						case "selectagood":
							int number=(Integer)result.getObj();
							Goods good=IFactory.selectAGoods(number);
							if(good!=null)
							{
								rs=new Result<Object>(true,"找到了",good);
							}else{
								rs=new Result<Object>(false,"找不到",null);
							}
							break;
						case "getcart":						
							Client c1=IFactory.selectAClient(result.getObj().toString());
							Carts car=IFactory.selectABuyList(c1);
							if(car!=null)
							{
								rs=new Result<Object>(true,"找到了",car);
							}else{
								rs=new Result<Object>(true,"找不dao",null);
							}
							
							break;
						case "addcarts"	:
							Carts car1=(Carts)result.getObj();
							Client c2=IFactory.selectAClient(car1.getUsername());
							//System.out.println("(((((((((((())))"+c2);
							boolean a=IFactory.addACarts(car1, c2);
							//System.out.println("(((((((((((())))"+a);
							rs=new Result<Object>(a,"成功",null);
							break;
						case "deletecarts":
							Client cli=(Client)result.getObj();
							boolean a1=IFactory.deleteACarts(cli);
							rs=new Result<Object>(a1,"成功",null);
							break;
					}
					
					
					writer = new ObjectOutputStream(client.getOutputStream());
					writer.writeObject(rs);
					System.out.println(result);
					
				} catch (IOException | ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					//close
					try {
						if(writer!=null)
							writer.close();
						if(reader!=null)
							reader.close();
						if(client!=null)
							client.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
		
		
	}

}
